package cn.droidlover.xdroidmvp.mvp;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.kit.KnifeKit;
import cn.droidlover.xdroidmvp.util.ScreenUtils;

/**
 * Activity 基类  已实现以下功能
 * <p>
 * 1.不需要刷新的activity
 * 2.切换日夜模式
 * 3.检测横竖屏
 * 4.显示/隐藏Loading弹框
 * 5.ButterKnife 绑定数据
 * 6.控制RxJava生命周期，防止内存泄漏
 * 7.MVP模式 参考 https://github.com/north2016/T-MVP
 * 需要时  并且使用泛型 <P extends BasePresenter> 为当前Presenter实例
 */
public class XBaseActivity<P extends IPresent> extends BaseActivity implements IView<P> {

    private P p;
    private Unbinder unbinder;
    private View mContextView;

    //默认需要设置paddingTop高度为状态栏高度
    private boolean shouldPaddingTop = true;

    protected int mPage = 1;

    /*** 每页请求数量 */
    protected int PAGE_SIZE = 20;
    /*** 页码，默认从1开始 */
    protected boolean isRefresh = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getP();
        if (getLayoutId() > 0) {
            mContextView = bindLayout();
            setContentView(mContextView);
            bindUI(null);
            bindEvent();
        }

        initData(savedInstanceState);
    }


    @Override
    public Context getContext() {
        return context;
    }

    @Override
    public void bindUI(View rootView) {
        unbinder = KnifeKit.bind(this);
    }

    private View bindLayout() {
        return  LayoutInflater.from(this).inflate(getLayoutId(), null);
    }

    protected P getP() {
        if (p == null) {
            p = newP();
        }
        if (p != null) {
            if (!p.hasV()) {
                p.attachV(this);
            }
        }
        return p;
    }

    @Override
    protected void onStart() {
        super.onStart();
        int statusBarHeight = ScreenUtils.getStatusBarHeight(this);
        //动态设置状态栏高度
        if (shouldPaddingTop) {
            mContextView.setPadding(0, statusBarHeight, 0, 0);
        }
        if (useEventBus()) {
            BusProvider.getBus().register(this);
        }
    }

    protected void setShouldPaddingTop(boolean shouldPaddingTop) {
        this.shouldPaddingTop = shouldPaddingTop;
    }

    /**
     * 显示加载框
     */
    @Override
    public void showProgressDialog() {
        getBaseDelegate().showProgressDialog();
    }


    /**
     * 显示加载框（带文字）
     */
    @Override
    public void showProgressDialog(String message) {
        getBaseDelegate().showProgressDialog(message);
    }

    /**
     * 隐藏加载框
     */
    @Override
    public void hideProgressDialog() {
        getBaseDelegate().hideProgressDialog();
    }
    @Override
    public boolean useEventBus() {
        return false;
    }

    @Override
    public P newP() {
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (getOptionsMenuId() > 0) {
            getMenuInflater().inflate(getOptionsMenuId(), menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (useEventBus()) {
            BusProvider.getBus().unregister(this);
        }
        if (getP() != null) {
            getP().detachV();
        }
        p = null;
        KnifeKit.unbind(unbinder);
    }



    @Override
    public int getOptionsMenuId() {
        return 0;
    }

    @Override
    public void bindEvent() {

    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }


    /**
     * 网络请求结束（无论成功还是失败）
     */

    @Override
    public void onRequestFinish() {

    }




}
